package com.ruoyi.enterprise.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.enterprise.domain.ComHot;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.enterprise.domain.ComTc;
import com.ruoyi.enterprise.service.IComTcService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 概念题材Controller
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@RestController
@RequestMapping("/enterprise/tc")
public class ComTcController extends BaseController {
    @Autowired
    private IComTcService comTcService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 查询概念题材列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tc:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComTc comTc) {
        startPage();
        List<ComTc> list = comTcService.selectComTcList(comTc);
        return getDataTable(list);
    }

    /**
     * 导出概念题材列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tc:export')")
    @Log(title = "概念题材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ComTc comTc) {
        List<ComTc> list = comTcService.selectComTcList(comTc);
        ExcelUtil<ComTc> util = new ExcelUtil<ComTc>(ComTc.class);
        util.exportExcel(response, list, "概念题材数据");
    }

    /**
     * 获取概念题材详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tc:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(comTcService.selectComTcById(id));
    }

    /**
     * 新增概念题材
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tc:add')")
    @Log(title = "概念题材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComTc comTc) {
        return toAjax(comTcService.insertComTc(comTc));
    }

    /**
     * 修改概念题材
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tc:edit')")
    @Log(title = "概念题材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComTc comTc) {
        return toAjax(comTcService.updateComTc(comTc));
    }

    /**
     * 删除概念题材
     */
    @PreAuthorize("@ss.hasPermi('enterprise:tc:remove')")
    @Log(title = "概念题材", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(comTcService.deleteComTcByIds(ids));
    }

    /**
     * @param comHot
     */
    public void setUserId(ComTc comHot) {
        // 获取角色名
        LoginUser loginUser = tokenService.getLoginUser(request);
        List<SysRole> roleName = loginUser.getUser().getRoles();
        for (SysRole sysRole : roleName) {
            if (sysRole.isAdmin()) {
//                comHot.setUserId(null);
                return;
            } else {
//                comHot.setUserId(loginUser.getUserId());
            }
        }
    }

}
